package com.carbon.carbonmanager.service;

import com.carbon.carbonmanager.mapper.EnterpressMapper;
import com.carbon.carbonmanager.model.EnterpressInfo;
import com.carbon.carbonmanager.model.UserInfo;
import com.carbon.carbonmanager.unit.Pager;
import com.carbon.carbonmanager.unit.PagerData;
import com.carbon.carbonmanager.unit.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * 企业信息管理实现层
 * */
@Service
public class EnterpressService {
    @Autowired
    EnterpressMapper enterpressMapper;

    @Autowired
    UserService userService;

    /**
     * 查询所有企业所属行业分类
     * */
    public Response getAllIndustry(){
        return new Response(200,"获取行业信息成功",enterpressMapper.getAllIndustry());
    }

    /**
     * 添加企业信息用户开户申请
     * @return 返回值是error的值，如果为0则执行成功
     * */
    public Response ApplyAccount(EnterpressInfo enterpressInfo){
        //申请开户前验证用户状态
        UserInfo user = enterpressMapper.validateApplyAccount(enterpressInfo);
        if(user.getUser_state()!=0){
            return new Response(201,"该账户已经申请，请勿重复申请",null);
        }
        if(enterpressMapper.ApplyAccount(enterpressInfo)==0){
            //如果错误为0则执行成功
            return new Response(200,"成功提交开户申请",null);
        }else{
            //执行失败，事务回滚
            return new Response(203,"提交开户申请失败，请与管理员联系",null);
        }
    }

    /**
     * 申请开户前的验证方法
     * 如果用户状态是非0时，提示用户不能操作
     * */

    /**
     * 验证账户开户状态
     * @param user_id
     * */
    public Response validateApplyAccount(EnterpressInfo enterpressInfo){
        UserInfo user = enterpressMapper.validateApplyAccount(enterpressInfo);
        //获取查询状态结果
        if(user.getUser_state()!=1){
            //只要不是开通状态，返回结果就是状态信息
            return new Response(201,getUserStateMsg(user),user.getUser_state());
        }else{
            //已经开通，则加载企业信息返回给客户端
            return new Response(200,"用户已开通，正在加载企业信息",enterpressMapper.getEnterInfoByUserId(enterpressInfo));
        }
    }

    /**
     * 检查用户状态后推送的消息
     * */
    private String getUserStateMsg(UserInfo userInfo){
        Integer user_state = userInfo.getUser_state();
        switch (user_state){
            case 0:
                return "该账户未开户";
            case 1:
                return "该账户已开户";
            case 2:
                return "该账户申请中";
            case 3:
                return "该账户申请已驳回";
            case 6:
                return "该账户已禁用";
            default:
                return "数据异常";
        }
    }

    /**
     * 获取开户申请中的企业信息数据列表
     * */
    public Response getApplyingEnterprise(){
        return new Response(200,"查询成功",enterpressMapper.getApplyingEnterprise());
    }

    /******************************************************
     * 获取开户申请中的企业信息分页数据1
     * 获取分页数据包含查询
     * */
    private List<EnterpressInfo> getApplyingEnterpriseDataByPager(Pager pager){
        Integer currentPage = pager.getCurrentPage();
        Integer pageSize = pager.getPageSize();
        Integer startIndex = (currentPage - 1)*pageSize;
        pager.setStartIndex(startIndex);
        return enterpressMapper.getApplyingEnterpriseDataByPager(pager);
    }

    /**
     * 获取开户申请中的企业信息分页数据2
     * 获取分页数据总条数用于计算总页码
     * */
    private Integer getApplyingEnterpriseDataCounts(Pager pager){
        return enterpressMapper.getApplyingEnterpriseDataCounts(pager);
    }

    /**
     * 获取开户申请中的企业信息分页数据3
     * 计算总页码
     * */
    private Integer getApplyingEnterpriseTotalPages(Pager pager){
        Integer totalCounts = getApplyingEnterpriseDataCounts(pager);
        Integer pageSize = pager.getPageSize();
        Integer totalPage = 1;
        if(totalCounts%pageSize>0){
            totalPage = totalCounts/pageSize + 1;
        }else{
            totalPage = totalCounts/pageSize;
        }
        return totalPage;
    }

    /**
     * 获取开户申请中的企业信息分页数据4
     * 装载数据
     * */
    public Response getApplyingEnterpriseDataMapper(Pager pager){
        PagerData pagerData = new PagerData();
        pagerData.setData(getApplyingEnterpriseDataByPager(pager));
        pagerData.setPageTotal(getApplyingEnterpriseTotalPages(pager));
        return new Response(200,"加载申请中的用户数据成功",pagerData);
    }


    /**
     * 审核开户状态，设置开户
     * @return errer = 0 执行成功
     * */
    public Response CheckAccount(EnterpressInfo enterpressInfo){
        UserInfo userInfo = new UserInfo();
        userInfo.setUser_id(enterpressInfo.getV_user_id());
        //验证用户是否有操作该方法的权限
        if(!userService.validateUserOperation(userInfo)){
            return new Response(203,"无操作权限",null);
        }
        if(enterpressMapper.CheckAccount(enterpressInfo)==0){
            //更新开户状态成功，设置用户状态为1
            return new Response(200,"开户成功",null);
        }else{
            return new Response(203,"存储过程执行异常",null);
        }
    }

    /**
     * 根据用户id获取企业碳资产数据
     * */
    private EnterpressInfo getEnterprisePropertyData(EnterpressInfo enterpressInfo){
        return enterpressMapper.getEnterprisePropertyData(enterpressInfo);
    }

    /**
     * 获取碳资产信息及企业交易数据API接口
     * */
    public Response getEnterprisePropertyDataAndTradeData(EnterpressInfo enterpressInfo){
        HashMap<String,Object> hs = new HashMap<>();
        hs.put("propertyData",getEnterprisePropertyData(enterpressInfo));
        return new Response(200,"查询成功",hs);
    }
}
